{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dbcbef38",
   "metadata": {},
   "source": [
    "-------------------------------\n",
    "\n",
    "* 本学习记录人：林涵萩\n",
    "* 记录时间：2021-11-30\n",
    "* week14\n",
    "* 参考文档：\n",
    "> 1. [python官方文档](https://www.python.org/doc/)\n",
    "> 2. [许老师博客](https://www.cnblogs.com/xuzhichao)\n",
    "\n",
    "-------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07b5192b",
   "metadata": {},
   "source": [
    "# 本周重点知识\n",
    "\n",
    "## 1. 初识字典\n",
    "\n",
    "## 2. 字典的一般创建方法\n",
    "\n",
    "## 3. 字典的操作\n",
    "\n",
    "## 4. 实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69bf0f0f",
   "metadata": {},
   "source": [
    "# 1. 初识[字典](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "\n",
    "* Another useful data type built into Python is the dictionary (see Mapping Types — dict) **<front style=\"color red\">映射数据类型</front>**\n",
    "* 对比序列数据 vs 映射数据\n",
    ">> 1. 序列数据 index 是 从0开始，每次加一的数值作为索引值\n",
    ">> 2. 映射数据 index 是 **关键字**（不可变数据，例如字符串，数字，元组）\n",
    ">> 3. 序列数据是 **有顺序的**\n",
    ">> 4. 映射数据是 **无序的**\n",
    ">> 5. 序列数据 index 是不写在数据值中的，不显示的\n",
    ">> 6. 映射数据 index 作为关键字（key）写在数据结构中：{key:value}这种形式通常被人们成为**键值对（key:value pairs）**\n",
    "    \n",
    "* 总结字典的性质：\n",
    "> 1. 字典的key 是 唯一的（唯一性）\n",
    "> 2. 字典是没有顺序的（无序性）\n",
    "> 3. 字典的key是不可变的（不变性）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e0d75760",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初识字典\n",
    "tel = {'jack': 4098, 'sape': 4139}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6aa4d920",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'sape': 4139}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3e325b5",
   "metadata": {},
   "source": [
    "# 2.字典的一般创建方法\n",
    "> 1. 按照字典的数据结构直接创建\n",
    ">> * dict_name = {key1:value,key2:value2,key3,value3,...}\n",
    "> 2. 使用dict()方法创建字典\n",
    ">> * dict_name = dict(key=value1,key2:value2,key3,value3,...)\n",
    "> 3.使用字典新增的方式创建字典\n",
    ">> * dict_name[key1] = value1,dict_name[key2] = value2,dict_name[key3] = value3,..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eb578bbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "stud_info = {\n",
    "    \"2100001\":\"Alex\",\n",
    "    \"2100002\":\"Jack\",\n",
    "    \"2100003\":\"LiHua\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb265305",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'LiHua'}\n"
     ]
    }
   ],
   "source": [
    "print(type(stud_info))\n",
    "print(stud_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5bdd1d1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info[\"2100002\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "299532e8",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-8-685569107426>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-8-685569107426>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    http://httpbin.org/get\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 练习查询\n",
    "http://httpbin.org/get\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5324576f",
   "metadata": {},
   "outputs": [],
   "source": [
    "get_dict = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61a58fdd-6e7ca4b60f97d3d408721a52\"\n",
    "  }, \n",
    "  \"origin\": \"61.242.54.80\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a1e811bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       "  'Host': 'httpbin.org',\n",
       "  'Upgrade-Insecure-Requests': '1',\n",
       "  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-61a58fdd-6e7ca4b60f97d3d408721a52'},\n",
       " 'origin': '61.242.54.80',\n",
       " 'url': 'http://httpbin.org/get'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6256c3e4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'args': {}, 'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61a58fdd-6e7ca4b60f97d3d408721a52'}, 'origin': '61.242.54.80', 'url': 'http://httpbin.org/get'}\n"
     ]
    }
   ],
   "source": [
    "print(type(get_dict))\n",
    "print(get_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c73b54df",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gzip, deflate'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_dict[\"headers\"][\"Accept-Encoding\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d3a19ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 使用dict()方法创建字典（局限性：key 是以 变量名的方式进行赋值的）\n",
    "stud_info2 = dict(\n",
    "     name1 = \"Alex\",\n",
    "     name2 = \"Jack\",\n",
    "     name3 = \"LiHua\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bd0f821c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name1': 'Alex', 'name2': 'Jack', 'name3': 'LiHua'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "10fc5b47",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 使用字典新增的方式创建字典\n",
    "stud_info3 = {}\n",
    "stud_info3[\"2100001\"] = \"Alex\"\n",
    "stud_info3[\"2100002\"] = \"Jack\"\n",
    "stud_info3[\"2100003\"] = \"LiHua\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b83cc71d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'LiHua'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85e13df3",
   "metadata": {},
   "source": [
    "# 3. 字典的操作\n",
    "\n",
    "> 1. 字典的增删改查\n",
    ">> 1. 字典的查询\n",
    ">> 2. 字典的增加\n",
    ">> 3. 字典的修改\n",
    ">> 4. 字典的删除\n",
    "\n",
    "> 2. 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "63caf67e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack\n"
     ]
    }
   ],
   "source": [
    "# 1. 字典的查询 方法一\n",
    "if '2100002' in stud_info3:\n",
    "    print(stud_info3['2100002'])\n",
    "else:\n",
    "    print('2100004' + \"不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b8c21ca3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'2100004'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-8109a075082a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 如果查询不存在，KeyError:'2100004'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstud_info3\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'2100004'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '2100004'"
     ]
    }
   ],
   "source": [
    "# 如果查询不存在，KeyError:'2100004'\n",
    "print(stud_info3['2100004'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "55a7af22",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# 1. 字典的查询 方法二 get()\n",
    "print(stud_info3.get('2100004'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "23f5bd2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "if stud_info3.get('2100004') == None:\n",
    "    stud_info3['2100004'] = \"Marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6e1489ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2100001': 'Alex', '2100002': 'Xiaoming', '2100003': 'LiHua', '2100004': 'Marry'}\n"
     ]
    }
   ],
   "source": [
    "print(stud_info3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8402f351",
   "metadata": {},
   "source": [
    "> 3. 字典的改变\n",
    ">> 1. dict_name[key]=value\n",
    ">> 2. 注意：如果字典的key不存在，则新增，如果key存在，则修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "09684c61",
   "metadata": {},
   "outputs": [],
   "source": [
    "if '2100004' in stud_info3:\n",
    "    stud_info3['2100004'] = \"Xiaoming\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "19977623",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2100001': 'Alex', '2100002': 'Xiaoming', '2100003': 'LiHua', '2100004': 'Xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "print(stud_info3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3503fce",
   "metadata": {},
   "source": [
    "> 4. 字典的删除\n",
    ">> 1. pop()\n",
    ">>> 1. D.pop(k[,d])\n",
    ">>> 2.  remove **specified key**（指定的key关键字） and return the **corresponding value**（返回删除的value值）.\n",
    ">> 2. popitem:随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "efbc8cca",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_info3.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a227a19c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'LiHua'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 因为字典是无序的，所以没有最后一个元素，pop指定关键字删除\n",
    "stud_info3.pop('2100003')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "78a7cca0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Xiaoming', '2100004': 'Marry'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ba18e26a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_info3.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d20c029f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('2100004', 'Xiaoming')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3.popitem()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
